package CutLoad.transitions
{
	import CutLoad.Base;
	
	import com.greensock.TweenLite;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	
	/**
	 * 操作一组电影实例，当鼠标左右移动的时候，远近物体移动出现景深的效果。
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 下午01:33:31 
	 *
	 */
	public class MouseMoveLayerDepth extends Base
	{
		private var mouseMoveX:Number,mouseParet:Sprite
		private var arrSp:Array,arrInitX:Array,arrPercent:Array;
		/**
		 * 初始化 
		 * @param mouseParet 需要参考的mouseX的对象。该类会调用：mouseParet.mouseX
		 * @param arrSp 需要操作的元件实例数组
		 * @param arrPercent 每个元件效果参数数组。一般取值在1~100之间。值越大，幅度越左。
		 * 
		 */		
		public function MouseMoveLayerDepth(mouseParet:Sprite,arrSp:Array,arrPercent:Array)
		{
			if(mouseParet.stage==null) throw new Error("mouseParet.stage==null");
			if(arrSp.length!=arrPercent.length)  throw new Error("arrSp.length!=arrPercent.length");
			this.mouseParet=mouseParet;
			this.arrSp=arrSp;
			//开始所处的位置
			this.arrInitX=new Array();;
			for(var i:Number=0;i<arrSp.length;i++){
				arrInitX.push(arrSp[i].x);	
			}
			this.arrPercent=arrPercent;
			//
			//
			mouseParet.stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMove);
		}
		private function mouseMove(e:MouseEvent):void{
			var stageW:Number=mouseParet.stage.width;
			for(var i:Number=0;i<arrSp.length;i++){
				var movetox:Number=( mouseParet.stage.width / 2-mouseParet.mouseX ) / mouseParet.stage.width * arrPercent[i]+arrInitX[i]
				TweenLite.to(arrSp[i],1,{x:movetox});
			}
		}
		override public function killMe():void{
			if(mouseParet.stage!=null) if(mouseParet.stage.hasEventListener(MouseEvent.MOUSE_MOVE)) mouseParet.stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMove);
			super.killMe();
		}
	}
}